package com.yqg.education.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqg.education.common.Result;
import com.yqg.education.controller.dto.TrainClassDTO;
import com.yqg.education.entity.TeachPlan;
import com.yqg.education.entity.TrainClass;
import com.yqg.education.mapper.TrainClassMapper;
import com.yqg.education.service.ITrainClassService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TrainClassController 前端控制器
 * @author 张芸博
 * @since 2025-04-10
 */
@RestController
@RequestMapping("/train-class")
public class TrainClassController {

    @Resource
    private ITrainClassService trainClassService;
@Resource
private TrainClassMapper trainClassMapper;
    /**
    *  新增或者更新
    */
    @PostMapping("/edit")
    public Result save(@RequestBody TrainClass trainClass) {
        trainClass.setCreateTime(LocalDateTime.now());
        return Result.success(trainClassService.saveOrUpdate(trainClass));
    }
    /**
    *  删除
    */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        TrainClass trainClass =trainClassService.getById(id);
        trainClass.setIsDelete(1);
        return Result.success(trainClassService.saveOrUpdate(trainClass));
    }
    /**
    *  查询所有
    */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<TrainClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        return Result.success(trainClassService.list(queryWrapper));
    }
    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(trainClassService.getById(id));
    }
    /**
    *  分页查询
    */
//    @GetMapping("/page")
//    public Result findPage(@RequestParam Integer pageNum,
//                                @RequestParam Integer pageSize) {
//        pageNum = pageNum * pageSize - pageSize;
//        return Result.success(trainClassService.SelectAll(pageNum,pageSize));
//    }
    //    模糊查询
    @GetMapping("/likeSelect")
    public Result likeSelect(@RequestParam Integer pageNum,
                             @RequestParam Integer pageSize, String trainClassNum){
        pageNum = pageNum * pageSize - pageSize;
        List<TrainClass> trainClassList = trainClassService.LikeSelect(pageNum, pageSize, trainClassNum);
        Long count = trainClassMapper.findCount(trainClassNum);
        HashMap<String, Object> map = new HashMap<>();
        map.put("records", trainClassList);
        map.put("total", count);
        return Result.success(map);
    }
    /**
     * 后端查看活跃班级学员图表获取数据接口
     */
    @GetMapping("/findActiveClassAndStudent")
    public Result findActiveClassAndStudent(){
        Map<String,Object> ActiveInfo = new HashMap<>();
        List<Object> classInfo = new ArrayList<>();
        List<Object> value = new ArrayList<>();

        List<Map<String,Object>> activeClassAndStudentList = trainClassMapper.getActiveClassAndStudent();
        activeClassAndStudentList.forEach((map)->{
            classInfo.add(map.get("train_class_num"));
            value.add(map.get("value"));
        });
        ActiveInfo.put("classInfo", classInfo);
        ActiveInfo.put("value", value);
        return Result.success(ActiveInfo);
    }
}

